﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATM_Software
{
    class TransactionManager
    {
        TransactionDAL transationDAL;

        public TransactionManager()
        {
            transationDAL = new TransactionDAL();
        }

        // if customer passed in argument then it will return all transactions of a specif customer
        // otherwise it will return all transactions records in transactions.txt
        public List<TransactionData> GetAllTransactions(CustomerData customer = null)
        {
            List<TransactionData> allTransactions =  transationDAL.GetAllTransactions();
            List<TransactionData> specificTransactions = new List<TransactionData>();

            // if all transaction made by a specific cutomer required
            if (customer != null)
            {
                foreach (TransactionData record in allTransactions)
                {
                    if (customer.AccountId == record.AccountId)
                    {
                        specificTransactions.Add(record);
                    }
                }
                return specificTransactions;
            }
            return allTransactions;
        }
        public int TodaysTransactionsAmmount(CustomerData customer)
        {
            List<TransactionData> specificTransactions = this.GetAllTransactions(customer);
            int todaysTransactionAmmount = 0;
            foreach (TransactionData record in specificTransactions)
            {
                if (record.Date.Equals(DateTime.Now.ToString("dd/MM/yyyy")))
                {
                    todaysTransactionAmmount += record.Ammount;
                }
            }
            return todaysTransactionAmmount;
        }

        private TransactionData WithDrawHelper(ref CustomerData customer, int ammount)
        {
            TransactionData transaction = new TransactionData();
            transaction.AccountId = customer.AccountId;
            transaction.Ammount = ammount;
            transaction.Date = DateTime.Now.ToString("dd/MM/yyyy");
            transaction.TransactionType = "Withdrawn";
            transationDAL.Transaction(transaction);
            customer.Balance -= ammount;
            new CustomerManager().UpdateAccount(customer, false);
            return transaction;
        }

        public bool WithDraw(CustomerData customer)
        {
            if (customer.Balance > 0 && this.TodaysTransactionsAmmount(customer) != 20000)
            {
                Console.Clear();
                Console.WriteLine("\n==== Withdraw Cash ====\n");
                TransactionData transaction = new TransactionData();
                string choice = "";
                bool isWithDrawn = false;
                Console.WriteLine("a) Fast Cash");
                Console.WriteLine("b) Normal Cash");
                Console.Write("Please select one of the above options : ");
                Console.ForegroundColor = ConsoleColor.Green;
                choice = Console.ReadLine();
                Console.ResetColor();
                if (choice.Equals("a"))
                {                    
                    Console.WriteLine("1----500");
                    Console.WriteLine("2----1000");
                    Console.WriteLine("3----2000");
                    Console.WriteLine("4----5000");
                    Console.WriteLine("5----10000");
                    Console.WriteLine("6----15000");
                    Console.WriteLine("7----20000");
                    Console.Write("Select one of the denominations of money : ");
                    Console.ForegroundColor = ConsoleColor.Green;
                    choice = Console.ReadLine();
                    Console.ResetColor();
                    switch (choice)
                    {
                        case "1":
                            if (customer.Balance - 500 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.500 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer,500);
                                    isWithDrawn = true;                                                                    
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...",customer.Balance,500);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        case "2":
                            if (customer.Balance - 1000 >= 0)
                            {
                                Console.Write("Are you sure you want to withdraw Rs.1000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 1000);
                                    isWithDrawn = true;                                    
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...",customer.Balance,1000);
                                Console.WriteLine("Press any key to go back ...");
                            }
                            break;

                        case "3":
                            if (customer.Balance - 2000 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.2000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 2000);
                                    isWithDrawn = true;
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, 2000);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        case "4":
                            if (customer.Balance - 5000 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.5000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 5000);
                                    isWithDrawn = true;
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, 5000);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        case "5":
                            if (customer.Balance - 10000 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.10000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 10000);
                                    isWithDrawn = true;
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, 10000);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        case "6":
                            if (customer.Balance - 15000 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.15000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 15000);
                                    isWithDrawn = true;
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, 15000);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        case "7":
                            if (customer.Balance - 20000 >= 0)
                            {

                                Console.Write("Are you sure you want to withdraw Rs.20000 (Y/N) : ");
                                Console.ForegroundColor = ConsoleColor.Green;
                                if (Console.ReadLine().ToLower().Equals("y"))
                                {
                                    transaction = this.WithDrawHelper(ref customer, 20000);
                                    isWithDrawn = true;
                                }
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, 20000);
                                Console.WriteLine("Press any key to go back ...");
                                Console.ReadKey();
                            }
                            break;

                        default:
                            Console.WriteLine("Invalid Selection ...");
                            Console.WriteLine("Press any key to go back ...");
                            Console.ReadKey();
                            break;
                    }
                    if (isWithDrawn)
                    {
                        Console.WriteLine("Cash Successfully Withdrawn!");
                        Console.Write("\nDo you wish to print a receipt (Y/N) : ");
                        Console.ForegroundColor = ConsoleColor.Green;
                        if (Console.ReadLine().ToLower().Equals("y"))
                        {
                            Console.ResetColor();
                            Console.WriteLine("\n===Reciept===\n");
                            Console.WriteLine("Account number : " + transaction.AccountId);
                            Console.WriteLine("Date : " + transaction.Date);
                            Console.WriteLine("Withdrawn : " + transaction.Ammount);
                            Console.WriteLine("Balance : " + customer.Balance);
                            Console.Write("Press any key to go back ...");
                            Console.ReadKey();
                        }
                        return true;
                    }
                    return false;
                }
                else if (choice.Equals("b"))
                {                    
                    int ammount = 0;
                    Console.Write("Enter the withdrawal amount : ");
                    try
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        ammount = int.Parse(Console.ReadLine());
                        Console.ResetColor();
                        if (ammount > 20000 || ammount <= 0 || (customer.Balance - ammount) < 0 )
                        {
                            throw new Exception();
                        }                        	    
                        else
                        {
                            transaction = this.WithDrawHelper(ref customer, ammount);
                            Console.WriteLine("Cash Successfully Withdrawn!");                            
                            Console.Write("\nDo you wish to print a receipt (Y/N) : ");
                            Console.ForegroundColor = ConsoleColor.Green;
                            if (Console.ReadLine().ToLower().Equals("y"))
                            {                                
                                Console.ResetColor();
                                Console.WriteLine("\n===Reciept===\n");
                                Console.WriteLine("Account number : " + transaction.AccountId);
                                Console.WriteLine("Date : " + transaction.Date);
                                Console.WriteLine("Withdrawn : " + transaction.Ammount);
                                Console.WriteLine("Balance : " + customer.Balance);
                                Console.Write("Press any key to go back ...");
                                Console.ReadKey();
                            }                            
                        }
                    }
                    catch(Exception)
                    {
                        if ((customer.Balance - ammount) < 0 )
                        {
                            Console.WriteLine("You current balance {0} is less than {1} ...", customer.Balance, ammount);
                            Console.WriteLine("Press any key to go back ...");
                            Console.ReadKey();
                            return false;
                        }
                        Console.WriteLine("Error : Please Enter integer ammount not greater than 20000 ...");
                        Console.WriteLine("Press any key to go back ...");
                        Console.ReadKey();
                        return false;
                    }
                    return true;
                }
                Console.ResetColor();                
                Console.WriteLine("Error : Incorrect selection ...");
                Console.WriteLine("Press any key to go back ...");
                Console.ReadKey();                
                return false;
            }            
            else if(customer.Balance == 0)
            {                
                Console.WriteLine("You dont have money in your account to withdraw ...");
                Console.WriteLine("Press any key to go back ...");
                Console.ReadKey();
                return false;
            }
            else
            {                
                Console.WriteLine("You can not withdraw ammount greater than 20000 in a day ...");
                Console.WriteLine("Press any key to go back ...");
                Console.ReadKey();
                return false;
            }
        }// end of WithDraw()

        public bool Deposit(CustomerData customer)
        {
            int ammount = 0;
            Console.Clear();
            Console.WriteLine("\n==== Deposit Cash ====\n");
            Console.Write("Enter the cash amount to deposit : ");
            try
            {
                TransactionData transaction = new TransactionData();
                Console.ForegroundColor = ConsoleColor.Green;
                ammount = int.Parse(Console.ReadLine());
                Console.ResetColor();
                transaction.AccountId = customer.AccountId;
                transaction.Ammount = ammount;
                transaction.Date = DateTime.Now.ToString("dd/MM/yyyy");
                transaction.TransactionType = "Deposited";
                transationDAL.Transaction(transaction);
                customer.Balance += ammount;
                new CustomerManager().UpdateAccount(customer, false);
                Console.WriteLine("Cash Deposited Successfully ...");
                Console.Write("\nDo you wish to print a receipt (Y/N) : ");
                Console.ForegroundColor = ConsoleColor.Green;
                if (Console.ReadLine().ToLower().Equals("y"))
                {
                    Console.ResetColor();
                    Console.WriteLine("\n===Reciept===\n");
                    Console.WriteLine("Account number : " + transaction.AccountId);
                    Console.WriteLine("Date : " + transaction.Date);
                    Console.WriteLine("Withdrawn : " + transaction.Ammount);
                    Console.WriteLine("Balance : " + customer.Balance);
                    Console.Write("Press any key to go back ...");
                    Console.ReadKey();
                }  
                return true;                
            }
            catch(Exception)
            {
                Console.ResetColor();
                Console.WriteLine("Enter possitve integer valued ammount not more than 10 digits long ...");
                Console.WriteLine("Press any key to go back ...");
                Console.ReadKey();
                return false;
            }            
        } // end of deposit

        public bool TransferCash(CustomerData customer)
        {
            int amount = 0;
            Console.Clear();
            Console.WriteLine("\n==== Transfer Cash ====\n");
            Console.Write("Enter amount in multiples of 500 : ");
            try
            {
                Console.ForegroundColor = ConsoleColor.Green;
                amount = int.Parse(Console.ReadLine());
                Console.ResetColor();
                if (amount < 500 || (amount % 500) != 0)
                {
                    Console.WriteLine("Enter possitve integer valued ammount multiple of 500 and not more than 10 digits long ...");
                    Console.WriteLine("Press any key to go back ...");
                    Console.ReadKey();
                    return false;
                }
                
                CustomerData tempCustomer = new CustomerData();
                Console.Write("Enter the account number to which you want to transfer : ");
                Console.ForegroundColor = ConsoleColor.Green;
                int account = int.Parse(Console.ReadLine());
                Console.ResetColor();
                tempCustomer.AccountId = account;
                List<CustomerData> customerList = new CustomerManager().Search(tempCustomer,false);
                if (customerList.Count !=0)
                {
                    tempCustomer = customerList[0];
                    Console.Write("You wish to deposit Rs {0} in account held by {1} If this information is correct please re-enter the account number : "
                    , amount, tempCustomer.Name);
                    Console.ForegroundColor = ConsoleColor.Green;
                    int reType = int.Parse(Console.ReadLine());
                    Console.ResetColor();
                    if (account != reType)
                    {
                        Console.WriteLine("Re Typed Account mismacth");
                        Console.WriteLine("Press any key to go back ...");
                        Console.ReadKey();
                        return false;   
                    }

                    customer.Balance -= amount;                                        
                    TransactionData transfer = new TransactionData();
                    transfer.AccountId = customer.AccountId;
                    transfer.TransactionType = "Transfered";
                    transfer.Ammount = amount;
                    transfer.Date = DateTime.Now.ToString("dd/MM/yyy");
                    transationDAL.Transaction(transfer);
                    new CustomerManager().UpdateAccount(customer,false);

                    tempCustomer.Balance += amount;
                    transfer.AccountId = tempCustomer.AccountId;
                    transfer.TransactionType = "Recieved";
                    transfer.Ammount = amount;
                    transfer.Date = DateTime.Now.ToString("dd/MM/yyy");
                    transationDAL.Transaction(transfer);
                    new CustomerManager().UpdateAccount(tempCustomer, false);
                    Console.WriteLine("Transaction confirmed ... ");                    
                    Console.Write("\nDo you wish to print a receipt (Y/N) : ");
                    Console.ForegroundColor = ConsoleColor.Green;
                    if (Console.ReadLine().ToLower().Equals("y"))
                    {
                        Console.ResetColor();
                        Console.WriteLine("\n===Reciept===\n");
                        Console.WriteLine("Account number : " + customer.AccountId);
                        Console.WriteLine("Date : " + DateTime.Now.ToString("dd/MM/yyyy"));
                        Console.WriteLine("Amount Transfered : " + amount);
                        Console.WriteLine("Balance : " + customer.Balance);
                        Console.Write("Press any key to go back ...");
                        Console.ReadKey();
                    } 
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                Console.WriteLine("Enter possitve integer valued ammount ...");
                Console.WriteLine("Press any key to go back ...");
                Console.ReadKey();
                return false;
            }                        
        }
    }
}

